package routers

import (
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"moko/internal/controller/account"
	"moko/internal/request/middleware"
)

func SetupUser(router *gin.Engine) {

	// 配置 CORS 中间件
	corsConfig := cors.DefaultConfig()
	corsConfig.AllowOrigins = []string{"*"} // 允许所有来源的请求，可以根据需要进行调整
	corsConfig.AllowMethods = []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}
	corsConfig.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type", "Authorization"}

	// 使用 CORS 中间件
	router.Use(cors.New(corsConfig))

	loginGroup := router.Group("/account")
	{
		loginGroup.POST("/register", account.Register) // 注册
		loginGroup.POST("/login", account.Login)       // 登录

		// 登录中间件
		authorized := loginGroup.Group("/", middlewares.AuthMiddleware())
		{
			authorized.POST("/logout", account.Logout)    // 登出
			authorized.GET("/info/:id", account.UserInfo) // 用户详情
		}
	}
}
